from math import inf


class Solution:

    def minFlipsMonoIncr(self, s: str) -> int:
        l = [[0, 0]]
        for v in map(int, s):
            l.append([l[-1][0] + v, l[-1][1] + (v ^ 1)])
        ans = inf
        a = b = 0
        for i in range(len(s) - 1, -1, -1):
            v = int(s[i])
            a, b = a + v, b + (v ^ 1)  # 右侧变成0，右侧变成1
            ans = min(ans, l[i][0] + a, l[i][0] + b, l[i][1] + b)
        return ans


so = Solution()
print(so.minFlipsMonoIncr(s="010110"))

# 输入：s = "010110"
# 输出：2
# 解释：我们翻转得到 011111，或者是 000111。